package core.genetic.selection;

import java.util.Arrays;

import utility.Pair;

import core.Mode;
import core.genetic.Individual;
import core.genetic.Population;

public class SimpleSelection implements Selection{

	public void select(Population pop, double[] values) {
		int length = values.length;
		int indlen = pop.get(0).getLength();
		Pair<Individual, Double>[] par = (Pair<Individual, Double>[])new Pair<?,?>[values.length];
		for (int i=0; i<values.length; i++){
			par[i] = new Pair<Individual, Double>(pop.get(i), values[i]);
		}
		Arrays.sort(par, new Pair.SecondDescendingComparator<Individual, Double>());
		
		for (int i=0; i<length/2; i+=2){
			Individual ind = Mode.mode.crossover.crossover(par[i].first(), par[i+1].first());			
			par[length/2+i].first(ind);
			ind = Mode.mode.crossover.crossover(par[i+1].first(), par[i].first());			
			par[length/2+i+1].first(ind);
		}
		

		pop.setFirst(par, length);
		
//		
//		for (int i=length/2-1; i>=Mode.mode.elitismRate*length/2; i--){
//			Mode.mode.mutation.mutate(pop.get(i));
//		}

		for(int i = (int)(0.5+Mode.mode.elitismRate*pop.size());i<pop.size();i++) 			
			Mode.mode.mutation.mutate(pop.get(i));

			
	}

}
